﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RuEn
{
    /// <summary>
    /// Attribute class for Comparer Rules. Compare one value with another using the defined Criteria
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
    public class ComparerStringRule : CompareRuleBase
    {
        private string _errorMessage = "String Comparions failed !!!";
        private string _comparer = string.Empty;
        private ComparerEnum _operation;

        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <param name="comparer">Value to compare with</param>
        /// <param name="operation">Comparision Criteria</param>
        public ComparerStringRule(string comparer, ComparerEnum operation)
        {
            _comparer = comparer;
            _operation = operation;
        }

        /// <summary>
        /// Overloaded Constructor
        /// </summary>
        /// <param name="comparer">Value to compare with</param>
        /// <param name="ErrorMessage">Custom Error Message</param>
        /// <param name="operation">Comparision Criteria</param>
        public ComparerStringRule(string comparer, string ErrorMessage, ComparerEnum operation)
        {
            _comparer = comparer;
            _operation = operation;
            _errorMessage = ErrorMessage;
        }

        /// <summary>
        /// Public property for Custom Error Message
        /// </summary>
        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }

        /// <summary>
        /// Public Property for value to compare with
        /// </summary>
        public string Comparer
        {
            get { return _comparer; }
            set { _comparer = value; }
        }

        /// <summary>
        /// Public Property for Compare criteria
        /// </summary>
        public ComparerEnum Operation
        {
            get { return _operation; }
            set { _operation = value; }
        }

        public bool Validate(string value, string Comparer, string ErrorMessage, ComparerEnum operation)
        {
            if (!string.IsNullOrEmpty(value))
            {
                if (!string.IsNullOrEmpty(Comparer))
                {
                    switch (operation)
                    {
                        case ComparerEnum.Equel:
                            if (value.Trim().ToUpper() != Comparer.Trim().ToUpper())
                            {
                                BrokenRules.Add(ErrorMessage);
                                return false;
                            }
                            break;
                        case ComparerEnum.NotEquel:
                            if (value.Trim().ToUpper() == Comparer.Trim().ToUpper())
                            {
                                BrokenRules.Add(ErrorMessage);
                                return false;
                            }
                            break;
                        default:
                            if (value.Trim().ToUpper() != Comparer.Trim().ToUpper())
                            {
                                BrokenRules.Add(ErrorMessage);
                                return false;
                            }
                            break;
                    }
                }
            }
            return true;
        }
    }

    
}
